﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Ricciolo.Controls;
using System.Windows.Controls;
using System.Windows.Data;
using Csla.Core;
using OpenExpressApp.Module.WPF.Converter;
using OpenExpressApp.Editors;
using OpenExpressApp.MetaModel;

namespace OpenExpressApp.Module.WPF.Editors.TreeColumn
{
    public class EnumTreeColumn : TreeColumn
    {
        private EnumPropertyEditor _editor;

        public EnumTreeColumn(BusinessObjectPropertyInfo info, IListObjectView view)
            : base(info, view)
        {
            this._editor = new EnumPropertyEditor(this.PropertyInfo, this.View);
        }

        public override IWPFPropertyEditor Editor
        {
            get
            {
                return this._editor;
            }
        }

        public override FrameworkElement GenerateEditingFrameworkElement()
        {
            var control = base.GenerateEditingFrameworkElement();

            ComboBox result = control as ComboBox;
            if (result != null)
            {
                var value = this._editor.PropertyValue as Enum;
                var viewModel = new EnumViewModel(value);
                result.SelectedIndex = result.Items.IndexOf(viewModel);
            }

            return control;
        }

        protected override FrameworkElementFactory GenerateElementFactoryInBorder()
        {
            FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
            Binding textBinding = new Binding(PropertyInfo.Name);
            textBinding.Converter = new EnumConverter();
            textBlock.SetValue(TextBlock.TextProperty, textBinding);
            return textBlock;
        }
        protected override FrameworkElement GenerateElementInBorder()
        {
            TextBlock tb = new TextBlock();
            Binding textBinding = new Binding(PropertyInfo.Name);
            textBinding.Converter = new EnumConverter();
            tb.SetBinding(TextBlock.TextProperty, textBinding);
            return tb;
        }
    }
}
